jQuery(function($) {
	var setting = {
		callback: {
			onClick: openURLInTab
		},
		data: {
			key: {
				title: "title",
				url: "xUrl"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "pId",
				rootPId: 0
			}
		},
		view: {
			selectedMulti: false,
			autoCancelSelected: false,
			showTitle: true,
			expandSpeed: "slow"
		}
	};

	
	var zNodes =[
		{ id:1, pId:0, name:"Custom Icon 01", open:true, iconOpen:"script/jQuery1.7.2/ztree/css/img/diy/1_open.png", iconClose:"script/jQuery1.7.2/ztree/css/img/diy/1_close.png"},
		{ id:11, pId:1, name:"leaf node 01", icon:"script/jQuery1.7.2/ztree/css/img/diy/2.png"},
		{ id:12, pId:1, name:"leaf node 02", icon:"script/jQuery1.7.2/ztree/css/img/diy/3.png"},
		{ id:13, pId:1, name:"leaf node 03", icon:"script/jQuery1.7.2/ztree/css/img/diy/5.png"},
		{ id:2, pId:0, name:"Custom Icon 02", open:true, icon:"script/jQuery1.7.2/ztree/css/img/diy/4.png"},
		{ id:21, pId:2, name:"leaf node 01", icon:"script/jQuery1.7.2/ztree/css/img/diy/6.png"},
		{ id:22, pId:2, name:"leaf node 02", icon:"script/jQuery1.7.2/ztree/css/img/diy/7.png"},
		{ id:23, pId:2, name:"leaf node 03", icon:"script/jQuery1.7.2/ztree/css/img/diy/8.png"},
		{ id:3, pId:0, name:"no Custom Icon", open:true },
		{ id:31, pId:3, name:"leaf node 01"},
		{ id:32, pId:3, name:"leaf node 02"},
		{ id:33, pId:3, name:"leaf node 03"}
	];
	

	function openURLInTab(event, treeId, treeNode) {
		$tabs.tabs('add', treeNode.url, treeNode.name);
	}
	
	var user = {"username":"superadmin"};
	sendAjaxReq(JSON.stringify(user), function(data, textStatus, jqXHR) {
//		newNodes = treeObj.addNodes(null, data.treeNodes);
		$.fn.zTree.init($("#treeDemo"), setting, data.treeNodes);
	});
	
	
	var $tabs = $("#tabs").tabs({
		tabTemplate: "<li><a href='#{href}'>#{label}</a><span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
		add: function( event, ui ) {
			$tabs.tabs('select', '#' + ui.panel.id);
			
		},
		load: function(event, ui) {
			console.log("tab load successful");
		}
	});
	
	$("#tabs span.ui-icon-close").live("click",function() {
		var index = $("li", $tabs).index($(this).parent());
		$tabs.tabs("remove", index);
	});
	
});
